package com.datasecops.scannerservices.db;

import redis.clients.jedis.Jedis;

import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * Redis操作
 * @author: GeXiaoLong
 * @time: 2022/10/12 15:47
 */
public class Redis {
    private static Redis redis = null;

    /**
     * @param
     * @return
     * @Description
     * @author GeXiaoLong
     * @time 2022/10/13 15:47
     */
    public static List<String> getAllCollections(String id, int port, String password) {
        List<String> list = new ArrayList<>();
        list.add("string");
        return list;
    }

    /**
     * @param
     * @return
     * @Description
     * @author GeXiaoLong
     * @time 2022/10/13 15:47
     */
    public static LinkedHashMap<String, LinkedList<String>> connect(String id, int port, String password, String instant) {
        //连接本地的redis
        Jedis jedis = new Jedis(id, port);
        jedis.connect();
        if (password != null) {
            //如果有密码则需要下面这一行
            jedis.auth(password);
        }
        //查看服务是否运行,运行正常的话返回一个PONG，否则返回一个连接错误
        try {
            jedis.ping();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("服务运行失败");
        }

        int i = Integer.parseInt(instant);
        jedis.select(i);
        Set keys = jedis.keys("*");
        System.out.println(keys);
        List<String> list = new ArrayList<>();
        List<String> lists = new ArrayList<>();

        LinkedList<String> colsArray;
        LinkedHashMap<String, LinkedList<String>> sourceDataMap = null;
        sourceDataMap = new LinkedHashMap<>();
        for (Object key : keys) {
            String type = jedis.type((String) key);
            if ("string".equalsIgnoreCase(type)) {
                Object o = jedis.get((String) key);
                if (o == null) {
                    o = "";
                }
                colsArray = sourceDataMap.getOrDefault(key, new LinkedList<>());
                colsArray.add(o.toString());
                list.add(o.toString());

                Integer size = valueSize(o.toString());
                String data = size + "|";
                lists.add(data);
                sourceDataMap.put((String) key, colsArray);
                System.out.println(jedis.get((String) key));
            } else if ("list".equalsIgnoreCase(type)) {
                List<String> stringList = jedis.lrange((String) key, 0l, -1l);
                System.out.println(stringList);
            } else if ("hash".equalsIgnoreCase(type)) {
                System.out.println(jedis.hgetAll((String) key));
            } else if ("set".equalsIgnoreCase(type)) {
                System.out.println("获取集合中的成员" + jedis.smembers((String) key));
            } else if ("zset".equalsIgnoreCase(type)) {
                System.out.println("获取有序集合指定范围的元素" + jedis.zrange("set3", 0, -1));
            }
        }
        return sourceDataMap;
    }

    public static void close() {
        if (redis != null) {
            redis.close();
        }

    }

    public static int valueSize(String str) {
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
        int length = bytes.length;
        System.out.println(length);
        return length;
    }

    public static void main(String[] args) {
        connect("10.26.18.66", 6379, "123456789", "0");
        //getAllCollections("10.26.18.66",6379,"123456789");
        //souls("马大叔与小舅舅@");
    }


}

